package com.xinnet.mms.quartz.job;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import com.xinnet.core.utils.INetUtils;
import com.xinnet.mms.customtransfer.service.DealIntentionCustomerService;
import com.xinnet.mms.customtransfer.service.DealNewRegisterCustomerService;
import com.xinnet.mms.customtransfer.service.DealProtectCustomerService;
import com.xinnet.mms.customtransfer.service.DealSecondCustomerService;
import com.xinnet.mms.customtransfer.service.DealWaitChargeCustomerService;
import com.xinnet.mms.emp.service.HyMangerEmpDataHandleService;

@Service(value = "customTransferJob")
public class CustomTransferJob implements ApplicationContextAware{
	private static Logger log = LoggerFactory
			.getLogger(CustomTransferJob.class);
	
	private ApplicationContext applicationContext;

	@Autowired
	DealNewRegisterCustomerService dealNewRegisterCustomerService;
	
	@Autowired
	DealIntentionCustomerService dealIntentionCustomerService;
	
	@Autowired
	DealWaitChargeCustomerService dealWaitChargeCustomerService;
	
	@Autowired
	DealProtectCustomerService dealProtectCustomerService;
	
	@Autowired
	DealSecondCustomerService dealSecondCustomerService;
	// private static ExecutorService pool = Executors.newFixedThreadPool(5);
	/**
	 * 客户流转JOB
	 */
	public void execute() {

		try {
			String ip = INetUtils.getLocalHostAddress();
			log.info("运行流转任务IP：" + ip);
			
			//同步员工数据
			HyMangerEmpDataHandleService hyMangerEmpDataHandle = (HyMangerEmpDataHandleService)applicationContext.getBean("hyMangerEmpDataHandle");
			hyMangerEmpDataHandle.empSynchronous();
			// 客户流转
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String transferStartDate = sdf.format(new Date());
			log.info("**********************************************START CUSTOMER TRANSFER*****************************************************************************"
					+ transferStartDate);

//			// 生成服务期客户（流入服务期）
//			MakeOfServiceCustomService makeOfServiceCustomService = (MakeOfServiceCustomService) applicationContext
//					.getBean("makeOfServiceCustomService");
//			makeOfServiceCustomService.makeOfServiceCustomer();
//			
//			CustomSystematizationService customSystematizationService = (CustomSystematizationService)applicationContext.getBean("customSystematizationService");
//			customSystematizationService.getBaseData();
//			// 新注册客户流转（新客户流转到公海）
//			NewRegisterCustomTransferService newRegisterCustomTransferService = (NewRegisterCustomTransferService) applicationContext
//					.getBean("newRegisterCustomTransferService");
//			
//			// 回收客户流转（回收客户流入公海）
//			RetrieveCustomTransferService retrieveCustomTransferService = (RetrieveCustomTransferService) applicationContext
//					.getBean("retrieveCustomTransferService");
//			// 服务期客户流转（服务期客户流转到公海）
//			OfServiceCustomTransferService ofServiceCustomTransferService = (OfServiceCustomTransferService) applicationContext
//					.getBean("ofServiceCustomTransferService");
//			
//			//分页获取数据
//			newRegisterCustomTransferService.newRegisterCustomTransfer();
//			retrieveCustomTransferService.retrieveCustomTransfer();
//			ofServiceCustomTransferService.ofServiceCustomTransfer();
			
			dealNewRegisterCustomerService.dealwithNewCustomer();
			dealIntentionCustomerService.dealwithIntentionCustomer();
			dealWaitChargeCustomerService.dealWithWaitChargeCustomerService();
			dealProtectCustomerService.dealwithProtectCustomer();
			dealSecondCustomerService.dealwithSecondCustomer();
			
			String transferEndDate = sdf.format(new Date());
			log.info("**********************************************END CUSTOMER TRANSFER*****************************************************************************"
					+ transferEndDate);

		}  catch (SQLException e) {
			e.printStackTrace();
			log.error("customer transfer SQLException: ", e);
		} catch (Throwable e) {
			e.printStackTrace();
			log.error("customer transfer Exception: ", e);
		}
	}

	@Override
	public void setApplicationContext(ApplicationContext ctx)
			throws BeansException {
		applicationContext = ctx;
	}
}
